﻿
Imports LibMultiPlug.Core.Configuration
Imports LibMultiPlug.Core.Configuration.MultiPlugConfig


Namespace Services

    Public Class ServiceManager


#Region "Singleton Implementation"


        Private Shared _instance As ServiceManager


        Public Shared Function GetInstance() As ServiceManager

            If _instance Is Nothing Then _
                _instance = New ServiceManager()

            Return _instance

        End Function


#End Region


        Private _dataService As DbDataLib.DbDataService

        Private _benachService As ServiceDefinitions.BenachrichtigungsService

        Private _repoService As ServiceDefinitions.ReportService

        Private _dataAssembly As System.Reflection.Assembly

        Private _multiPlugConfig As LibMultiPlug.Core.Configuration.MultiPlugConfig


        Private Sub New()

            Call InternalInitialize()

        End Sub


        Private Sub InternalInitialize()

            _multiPlugConfig = _
                CType(System.Configuration.ConfigurationManager.GetSection(MULTI_PLUG_CONFIG_NAME), LibMultiPlug.Core.Configuration.MultiPlugConfig)

            _dataService = DbDataLib.DbDataRepository.GetInstance().GetDbDataService(_multiPlugConfig.ConnectionName, _
                                                                                     _multiPlugConfig.ConnectionString, _
                                                                                     _multiPlugConfig.ConnectionProvider)

            _dataAssembly = _
                    System.Reflection.Assembly.LoadFrom(_multiPlugConfig.DataAssemblyPath)

        End Sub


        Public Function GetRootDbDataService() As DbDataLib.DbDataService

            Return _dataService

        End Function


        Public Function GetBenachrichtigungsService() As ServiceDefinitions.BenachrichtigungsService

            If _benachService Is Nothing Then

                For Each st As ServiceType In _multiPlugConfig.ServiceTypes

                    If st.Name = "benachrichtigungen" Then
                        _benachService = CType( _
                            System.Activator.CreateInstance(_dataAssembly.GetType(st.type), New Object() {_dataService}),  _
                            ServiceDefinitions.BenachrichtigungsService)
                    End If

                Next

                If _benachService Is Nothing Then
                    Throw New NotImplementedException(String.Format("Es ist kein Service definiert der {0} implementiert.", _benachService.GetType().ToString()))
                End If

            End If

            Return _benachService

        End Function

        Public Function GetReportingService() As ServiceDefinitions.ReportService

            If _repoService Is Nothing Then

                For Each st As ServiceType In _multiPlugConfig.ServiceTypes

                    If st.Name = "reporting" Then

                        _repoService = CType( _
                            System.Activator.CreateInstance(_dataAssembly.GetType(st.type), New Object() {_dataService}),  _
                            ServiceDefinitions.ReportService)

                    End If

                Next

                If _repoService Is Nothing Then
                    Throw New NotImplementedException(String.Format("Es ist kein Service definiert der {0} implementiert.", _repoService.GetType().ToString()))
                End If

            End If

            Return _repoService

        End Function

    End Class

End Namespace